// pages/main/
import { Shop } from '../../models/shop'
import { Supply } from '../../models/supply'
import { Job } from '../../models/job'
import { Publish } from '../../models/other'

import { Bidding } from '../../models/bidding'
import { Candidate } from '../../models/candidate'

const biddingModel = new Bidding()
const shopModel = new Shop()
const supplyModel = new Supply()
const publish = new Publish()
const jobModel = new Job()
const candidateModel = new Candidate()

Page({
  data: {
    imgUrls: [],
    shop: [],
    supply: [],
    job: [
      { coverPic: '' },
      { coverPic: '' },
      { coverPic: '' },
      { coverPic: '' }
    ],
    showSkeleton: true,
    goods: [],
    tabs: ['求职', '招聘', '卖店', '供应商', '招标'],
    tabIndex: 0,
    recommendList: [],
    resumeList: [],
    recruitList: [],
    buyShopList: [],
    sellShopList: [],
    supplyList: [],
    biddingList: [],
    tagColors: {
      招聘: '#4C94FF',
      卖店: '#8E6FFF',
      供应商: '#1BCFCB',
      招标: '#FF5871'
    },
    swiperHeight: [],
    cardHeight: 360,
    pages: [1, 1, 1, 1, 1, 1],
    showPopup: false,
    showDialog: false
  },
  onShow() {
    this.getTabBar().init()
    this.setData({ showPopup: false })
  },
  onLoad() {
    this.getStoreClassify()
    this.homeRecommend()
    this.getRecruitList()
    this.getSellShopList()
    this.getSupplierList()
    this.getBiddingList()
  },
  onShowPopup() {
    this.setData({ showPopup: true })
  },
  onClose() {
    this.setData({ showPopup: false })
  },
  homeRecommend(type) {
    candidateModel.candidateList(
      {
        page: this.data.pages[0],
        count: 10
      },
      (res) => {
        this.setData({
          resumeList:
            type == 'more' ? [...this.data.resumeList, ...res.rows] : res.rows,
          showSkeleton: false,
          showDialog: !wx.getStorageSync('showHomeDialog')
        })
        this.setData({
          // 动态设置swiper高度
          'swiperHeight[0]': this.data.resumeList.length * 270
        })
        wx.stopPullDownRefresh()
      }
    )
  },

  getStoreClassify() {
    publish.getStoreClassify({}, (res) => {
      res.length = 10
      res.forEach(item => {
        if(item.categoryName == '洗浴盐膏SPA') item.categoryName = '洗浴盐膏'
      })
      this.setData({
        goods: res
      })
      console.log(res)
    })
  },

  getRecruitList(type) {
    jobModel.recruitList(
      {
        page: this.data.pages[1],
        count: 10
      },
      (res) => {
        this.setData({
          recruitList:
            type == 'more' ? [...this.data.recruitList, ...res.rows] : res.rows
        })
        const recruitNum =
          (this.data.recruitList.length % 2 === 0
            ? this.data.recruitList.length
            : this.data.recruitList.length + 1) / 2
        this.setData({
          'swiperHeight[1]': recruitNum * this.data.cardHeight
        })
      }
    )
  },

  getSellShopList(type) {
    shopModel.sellShopList(
      {
        page: this.data.pages[2],
        count: 10
      },
      (res) => {
        this.setData({
          sellShopList:
            type == 'more' ? [...this.data.sellShopList, ...res.rows] : res.rows
        })
        const sellShopNum =
          (this.data.sellShopList.length % 2 === 0
            ? this.data.sellShopList.length
            : this.data.sellShopList.length + 1) / 2
        this.setData({
          'swiperHeight[2]': sellShopNum * this.data.cardHeight
        })
      }
    )
  },

  getSupplierList(type) {
    supplyModel.supplyList(
      {
        page: this.data.pages[3],
        count: 10
      },
      (res) => {
        this.setData({
          supplyList:
            type == 'more' ? [...this.data.supplyList, ...res.rows] : res.rows
        })
        const supplyNum =
          (this.data.supplyList.length % 2 === 0
            ? this.data.supplyList.length
            : this.data.supplyList.length + 1) / 2
        this.setData({
          'swiperHeight[3]': supplyNum * this.data.cardHeight
        })
      }
    )
  },

  getBiddingList(type) {
    biddingModel.biddingList(
      {
        page: this.data.pages[4],
        count: 10
      },
      (res) => {
        this.setData({
          biddingList:
            type == 'more' ? [...this.data.biddingList, ...res.rows] : res.rows
        })
        const biddingNum =
          (this.data.biddingList.length % 2 === 0
            ? this.data.biddingList.length
            : this.data.biddingList.length + 1) / 2
        this.setData({
          'swiperHeight[4]': biddingNum * this.data.cardHeight
        })
      }
    )
  },

  onReachBottom() {
    const key = `pages[${this.data.tabIndex}]`
    this.setData({
      [key]: this.data.pages[this.data.tabIndex] + 1
    })
    console.log(this.data.pages)
    switch (this.data.tabIndex) {
      case 0:
        this.homeRecommend('more')
        break
      case 1:
        this.getRecruitList('more')
        break
      case 2:
        this.getSellShopList('more')
        break
      case 3:
        this.getSupplierList('more')
        break
      case 4:
        this.getBiddingList('more')
        break
    }
  },

  toNewPage(event) {
    console.log(event)
    let { id, type } = event.currentTarget.dataset
    console.log(id, type)
    switch (type) {
      case '招标':
        wx.navigateTo({
          url: `/pages/bidding-detail/bidding-detail?id=${id}`
        })
        break
      case '卖店':
        wx.navigateTo({
          url: `/pages/sellshop-detail/sellshop-detail?id=${id}`
        })
        break
      case '供应商':
        wx.navigateTo({
          url: `/pages/supply-detail/supply-detail?id=${id}`
        })
        break
      case '招聘':
        wx.navigateTo({
          url: `/pages/recruit-detail/recruit-detail?id=${id}`
        })
        break
      case '求职':
        wx.navigateTo({
          url: `/pages/candidate-detail/candidate-detail?id=${id}`
        })
        break
    }
  },

  toCach() {
    wx.navigateTo({
      url: `/packageA/pages/cash/cash`
    })
  },

  chooseTab(event) {
    let index = event.detail.current || event.target.dataset.index || 0
    this.setData({
      tabIndex: index
    })
  },

  setSwiperHeight() {},

  toStore() {
    wx.navigateToMiniProgram({
      appId: 'wxfe951dcab1dc4b81'
    })
  },

  toFree() {
    wx.navigateToMiniProgram({
      appId: 'wxfe951dcab1dc4b81',
      path: '/pages/freeTrial/index'
    })
  },

  toTech() {
    wx.navigateToMiniProgram({
      appId: 'wxfe951dcab1dc4b81',
      path: '/pages/todayHotStyle/index'
    })
  },

  toClassify() {
    wx.navigateToMiniProgram({
      appId: 'wxfe951dcab1dc4b81',
      path: '/pages/classify/index'
    })
  },

  toClockin() {
    wx.navigateToMiniProgram({
      appId: 'wxfe951dcab1dc4b81',
      path: '/pages/clockin/index'
    })
  },

  closeDialog() {
    this.setData({
      showDialog: false
    })
    wx.setStorageSync('showHomeDialog', true)
  },

  toDialog() {
    this.closeDialog()
    wx.navigateTo({
      url: `/pages/candidate/candidate`
    })
  },

  onPullDownRefresh() {
    this.setData({
      pages: [1, 1, 1, 1, 1, 1]
    })
    this.homeRecommend()
    this.getRecruitList()
    this.getSellShopList()
    this.getSupplierList()
    this.getBiddingList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: `/pages/main/main?otherId=${
        wx.getStorageSync('userInfo').user_id
      }&time=${+new Date()}`
    }
  }
})
